﻿<?xml version="1.0" encoding="utf-8" ?>

<cls:class
	name="NetworkConnection"
	type="http://metalx.org/Network/NetworkConnection"
	static="true"
	xmlns="http://metalx.org/Program"
	xmlns:cls="http://metalx.org/Class"
	xmlns:var="http://metalx.org/Variable"
	xmlns:clsf="http://metalx.org/i386/Functions/Class"
	xmlns:sys="http://metalx.org/i386/Functions/System"
	xmlns:cat="http://metalx.org/i386/Functions/Catalog"
	xmlns:obj="http://metalx.org/i386/Functions/Object"
	xmlns:mm="http://metalx.org/i386/Functions/MemoryManager"
	xmlns:bool="http://metalx.org/i386/Functions/Boolean"
	xmlns:str="http://metalx.org/i386/Functions/String"
	xmlns:lst="http://metalx.org/i386/Functions/List"
	xmlns:int="http://metalx.org/i386/Functions/Integer"
	xmlns:hex="http://metalx.org/i386/Functions/Hexadecimal"
	xmlns:pcif="http://metalx.org/i386/Functions/Pci"
	xmlns:net="http://metalx.org/i386/Functions/Network"
	xmlns:math="http://metalx.org/Intel/80386/Math"
	xmlns:ari="http://metalx.org/Intel/80386/Arithmetic"
	xmlns:shift="http://metalx.org/Intel/80386/Shift"
	xmlns:imm="http://metalx.org/Intel/80386/Immediate"
	xmlns:index="http://metalx.org/Intel/80386/Index"
	xmlns:cpu="http://metalx.org/Intel/80386/Operators"
	xmlns:op="http://metalx.org/Intel/80386/Operands">

	<cls:field name="NetworkDevice" type="http://metalx.org/Reference" offset="0"/>
	<cls:field name="InternetAddress" type="http://metalx.org/Network/Internet/InternetAddress" offset="4"/>
	<cls:field name="SubnetMask" type="http://metalx.org/Network/Internet/InternetAddress" offset="8"/>
	<cls:field name="GatewayInternetAddress" type="http://metalx.org/Network/Internet/InternetAddress" offset="12"/>
	<cls:field name="NameServerInternetAddress" type="http://metalx.org/Network/Internet/InternetAddress" offset="16"/>
	<cls:field name="GatewayNetworkAddress" type="http://metalx.org/Network/NetworkAddress" offset="20"/>
	<!--<cls:field name="NameServerNetworkAddress" type="http://metalx.org/Network/NetworkAddress" offset="26"/>-->

	<cls:method name="Create" type="http://metalx.org/Network/NetworkConnection" static="true">
		<cls:parameter name="NetworkDevice" type="http://metalx.org/Network/NetworkDevice"/>

		<mm:CheckOut length="32"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PullSIFromStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIAddress/>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddressPlusImmediate8/>
		<byte>4</byte>
		<int>0</int>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddressPlusImmediate8/>
		<byte>8</byte>
		<int>0</int>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddressPlusImmediate8/>
		<byte>12</byte>
		<int>0</int>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddressPlusImmediate8/>
		<byte>16</byte>
		<int>0</int>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddressPlusImmediate8/>
		<byte>20</byte>
		<int>0</int>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddressPlusImmediate8/>
		<byte>24</byte>
		<int>0</int>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddressPlusImmediate8/>
		<byte>28</byte>
		<int>0</int>

		<obj:Create length="32"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="Enable" type="http://metalx.org/Network/NetworkConnection">
		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<!--Get Network Device-->
		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddress/>

		<cpu:PushSIToStack/>

		<obj:ExecuteMethod name="GetNetworkAddress"/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<!--Send DHCP Discover Packet-->
		<clsf:ExecuteMethod class="ConfigurationPacket" method="Create"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>4</byte>

		<obj:ExecuteMethod name="SendPacket"/>

		<!--Wait for DHCP Offer Packet-->
		<label id="waitForOffer"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>4</byte>

		<obj:ExecuteMethod name="IsPacketAvailable"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopySIAddressToALAndIncrementSI/>

		<cpu:CompareALToImmediate/>
		<byte>0</byte>

		<cpu:LoopToRelative8WhileEqual/>
		<addressOf ref="waitForOffer" type="Relative8"/>

		<!--Get IP Address-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>4</byte>

		<obj:ExecuteMethod name="GetPacket"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetFieldValue name="InternetPacket"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetFieldValue name="DataPacket"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetFieldValue name="ConfigurationPacket"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:ExecuteMethod name="GetYourIPAddress"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<!--Send ARP Request For IP Address To Check For Conflicts-->

		<!--Save IP Address-->
		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>24</byte>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<!--Save Subnet Mask-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>4</byte>

		<obj:ExecuteMethod name="GetSubnetMask"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>24</byte>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>8</byte>

		<!--Save Gateway IP Address-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>4</byte>

		<obj:ExecuteMethod name="GetRouterAddress"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>28</byte>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>12</byte>

		<!--Save DNS IP Address-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>8</byte>

		<obj:ExecuteMethod name="GetNameServerAddress"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>28</byte>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>16</byte>

		<!--Send DHCP Request Packet-->

		<!--Wait for DHCP Acknowledge Packet-->

		<!--Send ARP Request For Gateway IP Address-->
		<cpu:CopyOperandToRegister/>
		<op:AX-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>20</byte>

		<cpu:CopyOperandToRegister/>
		<op:BX-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>4</byte>

		<cpu:CopyOperandToRegister/>
		<op:CX-IndexAddress/>
		<index:SP/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushAXToStack/>
		<cpu:PushBXToStack/>
		<cpu:PushCXToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<clsf:ExecuteMethod class="AddressPacket" method="Create"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>24</byte>

		<obj:ExecuteMethod name="SendPacket"/>

		<!--Wait for ARP Reply-->
		<label id="waitForReply"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>24</byte>

		<obj:ExecuteMethod name="IsPacketAvailable"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopySIAddressToALAndIncrementSI/>

		<cpu:CompareALToImmediate/>
		<byte>0</byte>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="waitForReply" type="Relative8"/>

		<!--Save Gateway Network Address-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>24</byte>

		<obj:ExecuteMethod name="GetPacket"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetFieldValue name="AddressPacket"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetFieldValue name="SenderNetworkAddress"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>28</byte>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-DIAddress/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>20</byte>

		<cpu:Operand16/>
		<cpu:CopyOperandToRegister/>
		<op:AX-DIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:Operand16/>
		<cpu:CopyRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>24</byte>

		<!--Send ARP Request For DNS IP Address-->

		<!--Wait for ARP Reply-->

		<!--Save DNS Network Address-->

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSPRegister/>
		<byte>28</byte>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetInternetAddress" type="http://metalx.org/Network/Internet/InternetAddress">
		<cls:parameter name="Name" type="http://metalx.org/String"/>

		<!--Save Network Connection-->
		<cpu:PushDIToStack/>

		<!--Save Network Device-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddress/>

		<cpu:PushSIToStack/>

		<!--Save Name-->
		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PullDIFromStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushDIToStack/>

		<!--Get Network Address-->
		<obj:ExecuteMethod name="GetNetworkAddress"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<!--Get Gateway Address-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>8</byte>

		<obj:GetFieldValue name="GatewayNetworkAddress"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<!--Get Internet Address-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>8</byte>

		<obj:GetFieldValue name="InternetAddress"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<!--Get Name Server Internet Address-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>8</byte>

		<obj:GetFieldValue name="NameServerInternetAddress"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>
		
		<!--Restore Name-->
		<cpu:PullDIFromStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>
		
		<!--Create Domain Packet-->
		<clsf:ExecuteMethod class="DomainPacket" method="Create"/>
		
		<!--Send Domain Packet-->
		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:PushDIToStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:SP-BPRegister/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<obj:ExecuteMethod name="SendPacket"/>
		
		<!--Wait For Response-->
		<label id="waitForResponse"/>
		
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<obj:ExecuteMethod name="IsPacketAvailable"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>
		
		<obj:GetData/>

		<cpu:MathImmediateToOperandFunction8/>
		<math:CompareWithSIAddress/>
		<byte>0</byte>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="waitForResponse" type="Relative8"/>
		
		<!--Get Response-->
		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<obj:ExecuteMethod name="GetPacket"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetFieldValue name="InternetPacket"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<cpu:PushSIToStack/>

		<obj:ExecuteMethod name="GetProtocol"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:MathImmediateToOperandFunction8/>
		<math:CompareWithSIAddress/>
		<byte>17</byte>

		<cpu:PullSIFromStack/>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="getDataPacket" type="Relative8"/>

		<cpu:JumpToRelative/>
		<addressOf ref="waitForResponse" type="Relative32"/>

		<label id="getDataPacket"/>
		
		<obj:GetFieldValue name="DataPacket"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<cpu:PushSIToStack/>

		<obj:ExecuteMethod name="GetSource"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>
		
		<obj:GetData/>

		<cpu:Operand16/>
		<cpu:MathImmediateToOperandFunction/>
		<math:CompareWithSIAddress/>
		<short>53</short>

		<cpu:PullSIFromStack/>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="getDomainPacket" type="Relative8"/>

		<cpu:JumpToRelative/>
		<addressOf ref="waitForResponse" type="Relative32"/>

		<label id="getDomainPacket"/>

		<obj:GetFieldValue name="DomainPacket"/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:ExecuteMethod name="GetInternetAddress"/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSPRegister/>
		<byte>8</byte>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetNetworkDevice" type="http://metalx.org/Network/NetworkDevice">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DI-SIAddress/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ToString" type="http://metalx.org/String">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetType/>

		<clsf:GetName/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<str:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class" static="true">
		<cpu:CopyRegisterToOperand/>
		<op:BX-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>
</cls:class>